<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stockcode">
              <a-input v-model="model.stockcode" placeholder="请输入编号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stockname">
              <a-input v-model="model.stockname" placeholder="请输入名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="状态属性" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stocktypelist">
              <j-search-select-tag placeholder="请选择状态属性" v-model="model.stocktypelist" 
              :dictOptions="dictOptions">
              </j-search-select-tag>
              {{ selectValue }}
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="可使用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="canuse">
              <a-checkbox v-model="model.canuse" placeholder="请输入单位"></a-checkbox>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="不可销售" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="notsale">
              <a-checkbox v-model="model.notsale" placeholder="请输入单位"></a-checkbox>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="不可领用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="notoutstock">
              <a-checkbox v-model="model.notoutstock" placeholder="请输入单位"></a-checkbox>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="可锁定库存" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="canlock">
              <a-checkbox v-model="model.canlock" placeholder="请输入单位"></a-checkbox>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="参与MRP计算" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="canmrp">
              <a-checkbox v-model="model.canmrp" placeholder="请输入单位"></a-checkbox>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="参与预警" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="canalter">
              <a-checkbox v-model="model.canalter" placeholder="请输入单位"></a-checkbox>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'ScmstocktypeForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 12 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 12 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {},
      dictOptions:[
        {
          text:'可用',
          value:'keyong'
        },
        {
          text:'待检',
          value:'daijian'
        },
        {
          text:'冻结',
          value:'dongjie'
        },
        {
          text:'退回冻结',
          value:'tuihuidj'
        },
        {
          text:'在途',
          value:'zaitu'
        },
        {
          text:'收货冻结',
          value:'shouhuodj'
        },
        {
          text:'废品',
          value:'feipin'
        },
        {
          text:'不良',
          value:'buliang'
        },
        {
          text:'不参与核算',
          value:'bucanyuhs'
        },
      ],
      url: {
        add: '/base/scmstocktype/add',
        edit: '/base/scmstocktype/edit',
        queryById: '/base/scmstocktype/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    }
  }
}
</script>
